package com.example.bluetooth;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends Activity {

	private List<WallObject> wallList;
	private LinearLayout wallListContainer;
	MyApp appState;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		wallListContainer = (LinearLayout)findViewById(R.id.wallListContainer);
		
		appState = ((MyApp)getApplicationContext());
		
		WallObject wall = new WallObject("MyFirstWall", 5, 1);
		wallList = new ArrayList<WallObject>();
		wallList.add(wall);
		refreshDisplay();

	}
	
	

	@Override
	protected void onRestart() {
		super.onRestart();
		appState = ((MyApp)getApplicationContext());
		wallList =  appState.getWallList();		
		refreshDisplay();
	}


	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onRestoreInstanceState(savedInstanceState);
		wallList = (ArrayList<WallObject>) savedInstanceState.getSerializable("wallList");
		
	}



	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState);
		outState.putSerializable("wallList", (Serializable)wallList);
		//for (WallObject w: wallList)
		//{
		//	outState.putSerializable(w.name, value)
		//}
	}



	private void refreshDisplay(){
		wallListContainer.removeAllViewsInLayout();
		for (WallObject w: wallList)
		{
			Button wallButton = new Button(this);
			wallButton.setId(wallList.indexOf(w));
			wallButton.setText(w.getWallName());
			wallButton.setOnClickListener(new Button.OnClickListener() {  
		        public void onClick(View v)
	            {
		        	Intent intent = new Intent(v.getContext(),WallGridActivity.class);
		        	appState.setWall(wallList.get(v.getId()));
		    		startActivity(intent);
		    		finish();
	            }
	         });
			wallListContainer.addView(wallButton);
		}
	}
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	public boolean configureNewWall(MenuItem item){
		Intent intent = new Intent(this,ConfigureNewWall.class);		
		startActivity(intent);
		finish();
		return true;
	}

}
